package com.rbac.web.impl;

import com.google.gson.Gson;
import com.rbac.web.common.Constant;
import com.rbac.web.model.User;
import com.rbac.web.service.IUserTokenService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author gengaoliu
 */
@Service
@RequiredArgsConstructor
public class UserTokenServiceImpl implements IUserTokenService {
  final StringRedisTemplate redisTemplate;
  final Gson gson;

  @Override
  public void saveUserRole(Integer userId, Integer roleId) {
    redisTemplate
        .opsForValue()
        .set(
            Constant.getUserRoleKey(userId),
            gson.toJson(roleId),
            Constant.TOKEN_EXPIRE_TIME,
            Constant.TOKEN_TIME_UNIT);
  }

  @Override
  public Boolean haveUserRole(Integer userId) {
    return redisTemplate.hasKey(Constant.getUserRoleKey(userId));
  }

  @Override
  public Integer getUserRole(Integer userId) {
    String s = redisTemplate.opsForValue().get(Constant.getUserRoleKey(userId));
    if (s == null) {
      return null;
    }
    return gson.fromJson(s, Integer.class);
  }

  @Override
  public void deleteUserRole(Integer userId) {
    redisTemplate.delete(Constant.getUserRoleKey(userId));
  }

  @Override
  public Boolean haveCode(String username) {
    return redisTemplate.hasKey(Constant.getUserNameKey(username));
  }

  @Override
  public void saveImageCode(String username, String code) {
    redisTemplate
        .opsForValue()
        .set(
            Constant.getUserNameKey(username),
            code,
            Constant.IMG_CODE_EXPIRE_TIME,
            Constant.IMG_CODE_EXPIRE_UNIT);
  }

  @Override
  public String getImgCode(String username) {
    return redisTemplate.opsForValue().get(Constant.getUserNameKey(username));
  }

  @Override
  public void saveToken(String token, User user) {
    redisTemplate
        .opsForValue()
        .set(
            Constant.getTokenKey(token),
            gson.toJson(user),
            Constant.TOKEN_EXPIRE_TIME,
            Constant.TOKEN_TIME_UNIT);
  }

  @Override
  public User getUser(String token) {
    String s = redisTemplate.opsForValue().get(Constant.getTokenKey(token));
    if (null == s) {
      return null;
    }
    return gson.fromJson(s, User.class);
  }

  @Override
  public void deleteImgCode(String username) {
    redisTemplate.delete(Constant.getUserNameKey(username));
  }

  @Override
  public void deleteToken(String token) {
    redisTemplate.delete(Constant.getTokenKey(token));
  }

  @Override
  public Boolean haveToken(String tokenId) {
    return redisTemplate.hasKey(Constant.getTokenKey(tokenId));
  }

  @Override
  public void saveAllUsers(List<User> users) {
    Map<String, String> collect =
        users.stream()
            .collect(Collectors.toMap((User user) -> String.valueOf(user.getId()), gson::toJson));
    redisTemplate.opsForHash().putAll(Constant.getUserKey(), collect);
  }
}
